﻿<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SPEditor.aspx.cs" Inherits="SPEditor.Layouts.SPEditor" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    
    <SharePoint:CssRegistration runat="server" Name="/_layouts/SPEditor/styles.css"></SharePoint:CssRegistration>
    <script src="/_layouts/SPEditor/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"> </script>

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    
    <div id="editor-top">
        <asp:UpdatePanel runat="server">
    
            <ContentTemplate>
                <asp:Panel runat="server" ID="PnlFileInfo" CssClass="PnlFileInfo">
                    <div class="col1">
                        <div class="fileInfoItem">
                            <span class="fileInfoLabel">Filename:</span> <asp:Label runat="server" ID="LblFileName"></asp:Label>
                        </div>
                        <div class="fileInfoItem">
                            <span class="fileInfoLabel">Title:</span> <asp:Label runat="server" ID="LblTitle"></asp:Label>
                        </div>
                        <div class="fileInfoItem">
                            <span class="fileInfoLabel">Author:</span> <asp:Label runat="server" ID="LblAuthor"></asp:Label>
                        </div>
                    </div>
    
                    <div class="col2">
                        <div class="fileInfoItem">
                            <span class="fileInfoLabel">File Size:</span> <asp:Label runat="server" ID="LblFileSize"></asp:Label>
                        </div>   
                        <div class="fileInfoItem">
                            <span class="fileInfoLabel">Checked Out Status:</span> <asp:Label runat="server" ID="LblCheckedOutStatus"></asp:Label>
                        </div>   
                        <div class="fileInfoItem" style="display: none;">
                            <span class="fileInfoLabel">Moderation Status:</span> <asp:Label runat="server" ID="LblModStat"></asp:Label>
                        </div>
                       <div class="fileInfoItem">
                            <span class="fileInfoLabel">Last Modified:</span> <asp:Label runat="server" ID="LblModifed"></asp:Label>
                        </div>
                    </div>
                </asp:Panel>   

                <asp:Panel runat="server" ID="PnlDocumentControls" CssClass="PnlDocumentControls">
                    <asp:Label runat="server" ID="LblErrorMessage"></asp:Label>
                    <div>
                        <asp:Button runat="server" CssClass="statusButtonSave" Text="Save" ID="BtnSave" OnClick="BtnSaveClick" OnClientClick="PrepareOutput()" />
                    </div>
                    <div>
                        <asp:Button runat="server" CssClass="statusButtonCancel" Text="Cancel" ID="BtnCancel" OnClick="BtnCancelClick" />    
                    </div>
                </asp:Panel>     
                
                <asp:Panel runat="server" ID="PnlStatusBar" CssClass="statusBar s4-status-s1" Visible="True">
                    <span class="statusBarLabel">Status: </span><asp:Label runat="server" ID="LblStatusMessage" CssClass="statusBarMessage"></asp:Label>
                    <asp:Image ID="ImgPreloader" CssClass="loaderImg" runat="server" ImageUrl="/_layouts/SPEditor/images/loader.gif"/>
                </asp:Panel>
            </ContentTemplate>    
        </asp:UpdatePanel>
    </div>

    <div id="editor-bottom">
        <asp:Literal runat="server" ID="LitFileContents"></asp:Literal>

        <asp:HiddenField runat="server" ID="HfFileContents"/>
        <asp:HiddenField runat="server" ID="HfFileMode"/>
        <asp:HiddenField runat="server" ID="HfReadOnly" Value=""/>

        <script type="text/javascript">
            
            var editor = ace.edit("editor");
            editor.setTheme("ace/theme/textmate");
            var editorModeValue = document.getElementById('<%= HfFileMode.ClientID %>').value;
            var editorMode = GetMode(editorModeValue);
            editor.getSession().setMode("ace/mode/" + editorMode);
            var readOnlyValue = document.getElementById('<%= HfReadOnly.ClientID %>').value;
            if (readOnlyValue.toLowerCase() == 'true') {
                Log("Editor Read Only Mode: " + readOnlyValue);
                editor.setReadOnly(true);    
            }

            //OnEditorPageLoad();
            
            function PrepareOutput() {
                var editorContents = editor.getValue();
                var newContents = document.getElementById('<%= HfFileContents.ClientID %>');
                newContents.value = editorContents;

                var loaderImg = document.getElementById('<%= ImgPreloader.ClientID %>');
                loaderImg.style.visibility = "visible";
                var status = document.getElementById('<%= LblStatusMessage.ClientID %>');
                status.innerHTML = "Saving";
            }

            function gup(name) {
                name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regexS = "[\\?&]" + name + "=([^&#]*)";
                var regex = new RegExp(regexS);
                var results = regex.exec(window.location.href);
                if (results == null)
                    return "";
                else
                    return results[1];
            }
            
            function GetMode(fileExt) {
                Log("FileExt: " + fileExt);
                var mode = "";
                switch (fileExt) {
                case "htm":
                    mode = "html";
                    break;
                case "txt":
                    mode = "text";
                    break;
                case "js":
                    mode = "javascript";
                    break;
                case "xsl":
                    mode = "xml";
                    break;
                default:
                    mode = fileExt;
                }
                Log("Mode: " + mode);
                return mode;
            }
            
            function Log(msg) {
                if (typeof console != "undefined") {
                    console.log(msg);
                }
            }

            function SetEditorSize() {
                Log("Setting Editor Size");

                var viewportHeight;
                var editorTopHeight;
                var titleRowHeight;
                var ribbonHeight;
                var aceOffset = 153;

                if (typeof window.innerHeight != 'undefined') {
                    viewportHeight = window.innerHeight;
                }
                else {
                    viewportHeight = document.documentElement.clientHeight;
                }

                titleRowHeight = document.getElementById("s4-titlerow").clientHeight;
                editorTopHeight = document.getElementById("editor-top").clientHeight;
                ribbonHeight = document.getElementById("s4-ribbonrow").clientHeight;

                var bottomHeight = viewportHeight - titleRowHeight - editorTopHeight - ribbonHeight - aceOffset;
                document.getElementById("editor-bottom").style.height = bottomHeight + "px";
                editor.resize();

                Log(viewportHeight + "-" + titleRowHeight + "-" + editorTopHeight + "-" + ribbonHeight + "=" + bottomHeight);
            }

            function OnEditorPageLoad() {
                SetEditorSize();
                window.onresize = SetEditorSize;
            }

        </script>
    </div>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    SharePoint Inline Editor
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
    SharePoint Inline Editor
</asp:Content>